package com.ia.aistream.manager.operating.util;

import com.alibaba.fastjson.JSONObject;
import com.ia.aistream.common.utils.StringUtils;
import com.ia.aistream.manager.framework.util.ShiroUtils;
import com.ia.aistream.manager.operating.conf.DocMapConf;
import com.ia.aistream.manager.operating.util.convert.DocHandleBusiness;
import com.ia.aistream.manager.operating.util.style.DocStyleUtil;
import com.ia.aistream.model.operating.dto.convert.ConvertDto;

import com.ia.aistream.model.operating.entity.*;
import com.ia.aistream.model.operating.entity.DocAppendix;
import com.spire.doc.Table;
import com.spire.doc.TableCell;
import com.spire.doc.collections.TableCollection;
import com.spire.doc.documents.Paragraph;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 实现处理表格嵌套的解析
 * 4-15：进行代码的重构
 * @author XiaoYu
 */
@Slf4j
public class DocSubTableUtil {

    /***
     * 获取表格中嵌套子表的数据
     * @param tables
     * @param tid
     * @return
     */
    public static List<DocContentTable> getSubTableContent(TableCollection tables, String tid) throws UnsupportedEncodingException {

        List<DocContentTable> result = new ArrayList<>();
        int index =0;
        // 循环子表的集合
        for (int coll=0; coll<tables.getCount(); coll++){
            Table table = tables.get(coll);
            // 循环行
            for (int r=0; r<table.getRows().getCount(); r++){
                // 获取行的列数
                int cols = table.getRows().get(r).getCells().getCount();
                // 创建对象，给对象属性赋值
                DocContentTable dct = new DocContentTable();
                dct.setDocTitleId(tid);
                dct.setRowNum((long)cols);
                dct.setCreateBy(ShiroUtils.getLoginName());
                dct.setCreateTime(new Date());
                dct.setRowType(0);
                dct.setStepNum(++index);
                // 循环列
                for(int c=0; c<cols; c++){
                    // 获取单元格对象
                    TableCell tableCell = tables.get(coll).getRows().get(r).getCells().get(c);
                    // 获取单元格内容
                    String tableCellContent = getTableCellContent(tableCell);
                    // 获取单元格样式
                    JSONObject columnStyle = DocStyleUtil.getColumnStyle(table, r, c);
                    // 获取单元格的状态,处理合并数据
                    String s = tableCell.getCellFormat().getVerticalMerge().toString();
                    if(s.equals("Continue")){
                        continue;
                    }
                    // 或区域单元格转义数据
                    ConvertDto tableConvertContent = DocHandleBusiness.getTableConvertContent(tableCell, 0);
                    String convert = null;
                    if(tableConvertContent!=null){
                        convert = tableConvertContent.toString();
                    }
                    switch (c){
                        case 0:
                            dct.setC1O(tableCellContent);
                            dct.setC1Style(columnStyle);
                            dct.setC1C(convert);
                            break;
                        case 1:
                            dct.setC2O(tableCellContent);
                            dct.setC2Style(columnStyle);
                            break;
                        case 2:
                            dct.setC3O(tableCellContent);
                            dct.setC3Style(columnStyle);
                            dct.setC3C(convert);
                            break;
                        case 3:
                            dct.setC4O(tableCellContent);
                            dct.setC4Style(columnStyle);
                            dct.setC4C(convert);
                            break;
                        case 4:
                            dct.setC5O(tableCellContent);
                            dct.setC5Style(columnStyle);
                            dct.setC5C(convert);
                            break;
                        default:
                            System.out.println("请联系管理员处理");
                            break;
                    }
                }
                // 将数据添加到字表集合中
                result.add(dct);
            }
        }

        return result;
    }

    /***
     * 获取附录表格中嵌套子表情况
     * @param tables 嵌套子表
     * @param tid 标题id
     * @return
     * @throws UnsupportedEncodingException
     */
    public static List<DocAppendix> getAppendixSubTableContent(TableCollection tables, String tid) throws UnsupportedEncodingException {

        List<DocAppendix> result = new ArrayList<>();
        int index =0;
        // 循环子表的集合
        for (int coll=0; coll<tables.getCount(); coll++){
            Table table = tables.get(coll);
            // 循环行
            for (int r=0; r<table.getRows().getCount(); r++){
                // 获取行的列数
                int cols = table.getRows().get(r).getCells().getCount();
                // 创建对象，给对象属性赋值
                DocAppendix dct = new DocAppendix();
                dct.setDocTitleId(tid);
                dct.setRowNum((long)cols);
                dct.setCreateBy(ShiroUtils.getLoginName());
                dct.setCreateTime(new Date());
                dct.setRowType(0L);
                dct.setPageFlag(0);
                dct.setTabFlag(0);
                dct.setStepNum(++index);
                // 循环列
                for(int c=0; c<cols; c++){
                    // 获取单元格对象
                    TableCell tableCell = tables.get(coll).getRows().get(r).getCells().get(c);
                    // 获取单元格内容
                    String tableCellContent = getTableCellContent(tableCell);
                    // 获取单元格样式
                    JSONObject columnStyle = DocStyleUtil.getColumnStyle(table, r, c);
                    // 获取单元格的状态,处理合并数据
                    String s = tableCell.getCellFormat().getVerticalMerge().toString();
                    if(s.equals("Continue")){
                        continue;
                    }
                    // 或区域单元格转义数据
                    ConvertDto tableConvertContent = DocHandleBusiness.getTableConvertContent(tableCell, 0);
                    String convert = null;
                    if(tableConvertContent!=null){
                        convert = tableConvertContent.toString();
                    }
                    switch (c){
                        case 0:
                            dct.setC1O(tableCellContent);
                            dct.setC1Style(columnStyle);
                            dct.setC1C(convert);
                            break;
                        case 1:
                            dct.setC2O(tableCellContent);
                            dct.setC2Style(columnStyle);
                            break;
                        case 2:
                            dct.setC3O(tableCellContent);
                            dct.setC3Style(columnStyle);
                            dct.setC3C(convert);
                            break;
                        case 3:
                            dct.setC4O(tableCellContent);
                            dct.setC4Style(columnStyle);
                            dct.setC4C(convert);
                            break;
                        case 4:
                            dct.setC5O(tableCellContent);
                            dct.setC5Style(columnStyle);
                            dct.setC5C(convert);
                            break;
                        default:
                            System.out.println("请联系管理员处理");
                            break;
                    }
                }
                // 将数据添加到字表集合中
                result.add(dct);
            }
        }
        return result;
    }

    /***
     * 实现段落中子表数据的解析
     * @param table
     * @param pid
     * @return
     */
    public static List<DocParagraphSub> getParagraphSub(Table table, String pid) throws Exception {

        List<DocParagraphSub> result = new ArrayList<>();
        // 获取四行中的最大行数
        int tableMaxCol = DocStyleUtil.getTableMaxCol(table);
        // 实现表格行的解析
        for (int r=0; r<table.getRows().getCount(); r++){
            // 获取行的列数
            int colNum = table.getRows().get(r).getCells().getCount();
            // 创建对象
            DocParagraphSub docParagraphSub = new DocParagraphSub();

            // 实现列的解析
            for (int c=0; c<colNum; c++){
                TableCell tableCell = table.getRows().get(r).getCells().get(c);
                // 获取单元格的状态,处理合并数据
                String s = tableCell.getCellFormat().getVerticalMerge().toString();
                if(s.equals("Continue")){
                    continue;
                }
                // 获取单元格内容
                String content = getTableCellContent(tableCell);
                // 获取单元格样式
                JSONObject columnStyle = DocStyleUtil.getAppendixColumnStyle(table, r, c, tableMaxCol);
                // 给其他属性赋值
                switch(c){
                    case 0:
                        docParagraphSub.setC1O(content);
                        docParagraphSub.setC1Style(columnStyle);
                        break;
                    case 1:
                        docParagraphSub.setC2O(content);
                        docParagraphSub.setC2Style(columnStyle);
                        break;
                    case 2:
                        docParagraphSub.setC3O(content);
                        docParagraphSub.setC3Style(columnStyle);
                        break;
                    case 3:
                        docParagraphSub.setC4O(content);
                        docParagraphSub.setC4Style(columnStyle);
                        break;
                    case 4:
                        docParagraphSub.setC5O(content);
                        docParagraphSub.setC5Style(columnStyle);
                        break;
                    case 5:
                        docParagraphSub.setC6O(content);
                        docParagraphSub.setC6Style(columnStyle);
                        break;
                    case 6:
                        docParagraphSub.setC7O(content);
                        docParagraphSub.setC7Style(columnStyle);
                        break;
                    case 7:
                        docParagraphSub.setC8O(content);
                        docParagraphSub.setC8Style(columnStyle);
                        break;
                    default:
                        System.out.println("数据暂未处理");
                }
            }
            // 给对象属性赋值
            docParagraphSub.setDocParagraphId(pid);
            docParagraphSub.setRowNum((long) colNum);
            docParagraphSub.setCreateBy(ShiroUtils.getLoginName());
            docParagraphSub.setCreateTime(new Date());
            result.add(docParagraphSub);
        }
        return result;
    }

    /**
     * 处理附录子表的数据
     * @param table
     * @return
     */
    public static Map<String, Object> getDocAppendixSub(Table table, String appId) throws UnsupportedEncodingException {
        List<DocAppendixSub> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        int index = 1;
        for (int r = 0; r < table.getRows().getCount(); r++) {
            DocAppendixSub tab=new DocAppendixSub();
            String trim = table.getRows().get(r).getCells().get(0).getParagraphs().get(0).getText().trim();
            if(trim.endsWith("附录") && table.getRows().get(r).getCells().getCount()==1){
                map.put("endRows", r);
                map.put(DocMapConf.DOC_APPENDIX_SUBTABLE_LIST.value, list);
                return map;
            }
            // 行的列数
            tab.setRowNum((long)table.getRows().get(r).getCells().getCount());
            //循环列
            for (int j = 0; j <table.getRows().get(r).getCells().getCount() ; j++) {
                TableCell tableCell = table.getRows().get(r).getCells().get(j);
                // 获取单元格内容
                String cellContent = DocIndex.getCellContent(tableCell).trim();
                // 获取单元格样式
                JSONObject columnStyle = DocStyleUtil.getAppendixColumnStyle(table, r, j);
                if(columnStyle==null){
                    continue;
                }
                String s = tableCell.getCellFormat().getVerticalMerge().toString();
                if(s.equals("Continue")){
                    columnStyle = null;
                }

                switch (j){
                    case 0:
                        tab.setC1O(cellContent);
                        tab.setC1Style(columnStyle);
                        break;
                    case 1:
                        tab.setC2O(cellContent);
                        tab.setC2Style(columnStyle);
                        break;
                    case 2:
                        tab.setC3O(cellContent);
                        tab.setC3Style(columnStyle);
                        break;
                    case 3:
                        tab.setC4O(cellContent);
                        tab.setC4Style(columnStyle);
                        break;
                    case 4:
                        tab.setC5O(cellContent);
                        tab.setC5Style(columnStyle);
                        break;
                    case 5:
                        tab.setC6O(cellContent);
                        tab.setC6Style(columnStyle);
                        break;
                    case 6:
                        tab.setC7O(cellContent);
                        tab.setC7Style(columnStyle);
                        break;
                    default:
                        log.error("附录子表格解析失败==" + j);
                        break;
                }
            }
            tab.setStepNum(index);
            tab.setDocAppendixParagraphId(appId);
            tab.setCreateBy(ShiroUtils.getLoginName());
            tab.setCreateTime(new Date());

            list.add(tab);
            index ++;
        }

        map.put(DocMapConf.DOC_APPENDIX_SUBTABLE_LIST.value, list);
        return map;
    }
    /**
     * 处理单元格中的内容
     * @param tableCell 单元格数据
     * @return
     */
    private static String getTableCellContent(TableCell tableCell){

        StringBuilder result = new StringBuilder();
        // 判断单元格中含有的段落数
        for(int p=0; p<tableCell.getParagraphs().getCount(); p++){
            // 获取段落对象
            Paragraph paragraph = tableCell.getParagraphs().get(p);
            // 获取列表数据+
            String content = paragraph.getListText().trim() + " " + paragraph.getText().trim();
            if(!StringUtils.isEmpty(content)){
                result.append(content);
            }
        }
        return result.toString();
    }
}